package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test5 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test4.class.getResource(".").toURI());
        File[] files = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File file : files) {
            String classname = file.getName().replaceAll(".class", "");
            String filename = Test4.class.getPackage().getName() + "." + classname;
            Class cls = Class.forName(filename);
            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                Object o = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(AutoRunMethod.class)) {
                        // AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                        int num = method.getAnnotation(AutoRunMethod.class).value();
                        for (int i = 0; i < num; i++) {
                            method.invoke(o);
                        }
                    }
                }
            }
        }
    }
}
